﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



using System.Net.Http;
using Newtonsoft.Json.Linq;

namespace OCX2_SAMPLE1_2
{
    public partial class Login : Form
    {
        public static string globalToken;

        public Login()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("登录成功！");
            //Program.isValidUser = true;

            //this.Close();
            //        /**



            //1. 获取数据
            string userName = this.textBox1.Text;
            string userPassword = this.textBox2.Text;

            var username = "zssm";
            var password = "Tdtxpassword@123";

            var requestUrl = "http://192.168.110.244:8029/login";

            var loginData = new { username = username, password = password };
            var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(loginData), Encoding.UTF8, "application/json");

            using (var httpClient = new HttpClient())
            {
                // 设置 User-Agent 头部信息
                httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("User-Agent");

                try
                {
                    var response = await httpClient.PostAsync(requestUrl, content);

                    if (response.IsSuccessStatusCode)
                    {
                        var responseContent = await response.Content.ReadAsStringAsync();
                        var responseObject = JObject.Parse(responseContent);
                        int statusCode = (int)responseObject["code"];

                        if (statusCode == 200)
                        {

                            // 提取token并存储在全局变量中
                            globalToken = (string)responseObject["data"]["token"];
                            MessageBox.Show("登录成功！");
                            Program.isValidUser = true;

                            this.Close();
                            //        /**
                            Console.WriteLine("Token stored globally: " + globalToken);
                        }
                        Console.WriteLine("Login successful!");
                        Console.WriteLine("Response Content: " + responseContent);
                    }
                    else
                    {
                        Console.WriteLine("Login failed. Status code: " + response.StatusCode);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred: " + ex.Message);
                }
            }














            ////2. 验证数据
            //// 验证用户输入是否为空，若为空，提示用户信息
            //if (userName.Equals("") || userPassword.Equals(""))
            //{
            //    MessageBox.Show("用户名或密码不能为空！");
            //}
            //// 若不为空，验证用户名和密码是否与数据库匹配
            //// 这里只做字符串对比验证
            //else
            //{
            //    //用户名和密码验证正确，提示成功，并执行跳转界面。
            //    if (userName.Equals("admin") && userPassword.Equals("admin"))
            //    {
            //        MessageBox.Show("登录成功！");
            //        Program.isValidUser = true;

            //        this.Close();
            //        /**
            //         * 待添加代码区域
            //         * 实现界面跳转功能
            //         * 
            //         */
            //    }
            //    //用户名和密码验证错误，提示错误。
            //    else
            //    {
            //        MessageBox.Show("用户名或密码错误！");
            //    }
            //}


            //}


        }
    }
}